package com.xxx.blog.service.impl;


import com.github.pagehelper.PageHelper;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.mapper.ArticleTypeMapper;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import com.xxx.vo.ArticleTypeStatistics;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArticleTypeServiceImpl implements IArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Override

    public List<ArticleType> findAll() {
        return articleTypeMapper.findAll();
    }

    @Override
    public ArticleType findOne(long id) {
        return articleTypeMapper.findOne(id);
    }

    @Override
    public List<ArticleType> queryPage(ArticleTypeQuery articleTypeQuery) {
        PageHelper.startPage(articleTypeQuery.getCurrentPage(), articleTypeQuery.getPageSize());
        return articleTypeMapper.queryPage(articleTypeQuery);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void addArticleType(ArticleType articleType) {
        articleTypeMapper.addArticleType(articleType);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void updateArticleType(ArticleType articleType) {
        articleType.setUpdateTime(new Date());
        articleTypeMapper.updateArticleType(articleType);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void deleteArticleType(long id) {
        articleTypeMapper.deleteArticleType(id);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void batchArticleTypes(long[] ids) {
        articleTypeMapper.batchArticleTypes(ids);
    }

    @Override
    //写缓存
    @Cacheable(cacheNames = "articleType", key = "'typeTree'")
    public List<ArticleType> getTypeTree(Long id) {
        return getArticleTypeTree(id);
    }

    private  List<ArticleType> getArticleTypeTree(Long id) {
        //类型树,用来存储一级文章类型
        List<ArticleType> typeTree = new ArrayList<>();
        //拿到所有类型数据,便于后续判断
        PageHelper.clearPage();
        List<ArticleType> articleTypeList = articleTypeMapper.findAll();
        //创建一个 Map 将类型 ID 与类型对象关联，方便后续快速查找父类型
        Map<Long, ArticleType> map = new HashMap<>();
        //给map赋值
        for (ArticleType articleType : articleTypeList) {
            map.put(articleType.getId(), articleType);
        }
        //遍历所有类型，将顶级类型（parentId 为 null）直接加入typeTree
        //非顶级类型则通过 parentId 找到其父类型，并添加到父类型的 children 集合中
        for (ArticleType articleType : articleTypeList) {
            //如果是自己的id跳过本次循环
            if (id.equals(articleType.getId())) {
                continue;
            }
            Long parentId = articleType.getParentId();
            if (parentId == null) {
                //把一级文章类型添加到类型树,父类型
                typeTree.add(articleType);
            } else {
                //非顶级类型则通过 parentId 找到其父类型，并添加到父类型的 children 集合中
                //如果parentId不为空,说明它不是一级文字类型,添加到子类型上
                ArticleType articleTypeParent = map.get(parentId);
                if (articleTypeParent != null) {
                    //3.获取父类型装子类型的集合children
                    List<ArticleType> children = articleTypeParent.getChildren();
                    //4.将自己装到集合中
                    children.add(articleType);
                }
            }
        }
        return typeTree;
    }

    @Override
    public List<ArticleTypeStatistics> findArticleTypeStatistics() {
        return articleTypeMapper.findArticleTypeStatistics();
    }
}
